<style>
.tree,.from{
    float: left;
}
.tree{
    width: 35%;
}
.from{
    width: 75%;
}
</style>

<template>
    <div>
        <div class="form">
            <Button type="primary" @click="modalForm = true">添加数据</Button>
            <Button type="primary" @click="removes">删除多个</Button>
            <Input search enter-button @on-search="search" placeholder="Enter something..." v-model="value"/>

            <Table border :columns="columns" :data="data" @on-selection-change="onSelect"></Table>
            <Page :total="total" :page-size="rows" @on-change="onChangePage" :page-size-opts=[5,10,15,20] @on-page-size-change="onPageSizeChange" show-sizer />

            <Modal v-model="modalForm" title="数据操作">
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="90">
                  <FormItem label="请选择图片" prop="img">
                     <!-- <Upload action="//jsonplaceholder.typicode.com/posts/">
                        <Button icon="ios-cloud-upload-outline" @change = "chooseImg" >请选择图片</Button>
                    </Upload> -->
                        <input  type="file" accept  = "image/*" @change = "chooseImg"  />
                </FormItem>
                <FormItem label="标题" prop="title">
                    <Input v-model="formValidate.title" placeholder="请输入标题"></Input>
                </FormItem>
                <FormItem label="内容" prop="content">
                    <Input v-model="formValidate.content" placeholder="请输入内容"></Input>
                </FormItem>
                <FormItem label="作者" prop="author">
                    <Input v-model="formValidate.author" placeholder="请输入作者"></Input>
                </FormItem>
                <FormItem label="来源" prop="source">
                    <Input v-model="formValidate.source" placeholder="请输入新闻来源"></Input>
                </FormItem>
                <FormItem label="发布日期" prop="publishDate">
                    <Row>
                        <Col span="12">
                            <DatePicker   type="date"
                            format      = "year"
                            placeholder = "Select date"
                            v-model     = "formValidate.publishDate"     style = "width: 200px"></DatePicker>
                        </Col>
                     </Row>

                </FormItem>
                <FormItem>
                    <Button type="primary" @click="handleSubmit('formValidate')">Submit</Button>
                    <Button @click="handleReset('formValidate')" style="margin-left: 8px">Reset</Button>
                </FormItem>
            </Form>
            <div slot="footer">
            </div>
          </Modal>
        </div>
    </div>

</template>
<script>
import Base from '../../base/Base';
export default {
  mixins: [Base],
  data() {
    return {
        treedata    : null,     //render树的数据
        module      : 'news',
        value       : null,
        data        : [],
        total       : 0,
        page        : 1,
        rows        : 5,
        modalForm   : false,
        ids         : [],
        formValidate: {
          title      : '',
          content    : '',
          author     : '',
          source     : '',
          publishDate: '',
          img        : '',
      },
      ruleValidate: {
        title: [
          {
            required: true,
            message : '必须输入标题',
            trigger : 'blur'
          }
        ],
        content: [
          {
            required: true,
            message : '必须输入新闻内容',
            trigger : 'blur'
          }
        ],
        author: [
          {
            required: true,
            message : '必须输入作者',
            trigger : 'blur'
          }
        ],
        source: [
          {
            required: true,
            message : '必须输新闻来源',
            trigger : 'blur'
          }
        ],
         publishDate: [
          {
            required: true,
            type    : 'date',
            message : '必须输入发布日期',
            trigger : 'change',
          }
        ]
       ,
      },
      columns: [
        {
          type : 'selection',
          width: 60,
          align: 'center'
        },

        {
          title: '标题',
          key  : 'title'
        },
        {
         title: '内容',
         key  : 'content'
        },
        {
            title: '作者',
            key  : 'author'
        },
        {
            title: '新闻来源',
            key  : 'source'
        },
        {
            title: '发布日期',
            key  : 'publishDate',
        },
        {
          title : '操作',
          key   : 'action',
          width : 150,
          align : 'center',
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                        console.log(params)
                      this.edit(params.row._id);
                    }
                  }
                },
                '修改'
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.remove(params.row._id);
                    }
                  }
                },
                '删除'
              )
            ]);
          }
        }
      ]

    };
  },
  methods:{
            chooseImg (event) {
            let file   = event.target.files[0]
            let reader = new FileReader()
            let img    = new Image()
            // 读取图片
            reader.readAsDataURL(file)
            // 读取完毕后的操作
            reader.onloadend = (e) => {
                img.src = e.target.result
                // 这里的e.target就是reader
                // console.log(reader.result)
                // reader.result就是图片的base64字符串
                this.formValidate.img = reader.result
            }
        },
      getTree(){
          this.axios({
              url   : 'http://10.31.162.52:3000/cates/list/1',
              method: 'post',
          }).then(res=>{
            //   console.log(res.data)
              this.treedata = res.data
          })
      },
      onSelectChange(node){
          console.log(node)
      },
      search(){
          this.axios({
              url   : `http://10.31.162.52:3000/news/list`,
              method: 'post',
              data  : {
                  title: this.value
              }
          }).then(res=>{
              this.data = res.data.rows;
              console.log(res)
          })
      },
  },
  mounted() {
    //   this.getData()
      this.getTree()
  },
}
</script>



